/**
 * Copyright (C) 2010 KingCore Science & Technology CO,.LTD. All rights reserved.
 * XNS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2010 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.director.core.util;

import java.awt.Component;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;

import org.apache.log4j.Logger;

/**
 * <p>java class file describe</p>
 * @author Zeven on 2010-7-1
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @since	JDK5
 */

public class DirectorComponentUtils {

	//log object
	private final static Logger log = Logger.getLogger(DirectorComponentUtils.class);
	


	public static void touchAllTabs(JTabbedPane pane){
		int cnt = pane.getTabCount();
		for (int i = 0; i < cnt; i++) {
			pane.setSelectedIndex( i );
		}
		pane.setSelectedIndex( 0 );
	}
	
	public static boolean isControlDown(KeyEvent e){
		return e.isControlDown();
	}
	
	public static boolean isControlDownOnly(KeyEvent e){
		return e.isControlDown()
				&& e.isAltDown()==false
				&& e.isShiftDown()==false;
	}

	public static boolean isShiftDown(ActionEvent e){
		//System.out.println(e.getModifiers()&ActionEvent.SHIFT_MASK);
		return (e.getModifiers() & ActionEvent.SHIFT_MASK) == ActionEvent.SHIFT_MASK;
	}
	
	public static void openWebUrl(String url){
		try {
			Desktop.getDesktop().browse( new URI(url) );
		} catch (IOException e) {
			log.error(e);
		} catch (URISyntaxException e) {
			log.error(e);
		}
	}

	public static SearchEngine _defaultWebSearch = null;
	public static SearchEngine getDefaultWebSearchProvider(){
		return _defaultWebSearch;  //GoogleSearch();  //
	}

	public static void setDefaultWebSearchProvider(SearchEngine defaultWebSearch){
		_defaultWebSearch = defaultWebSearch;  //GoogleSearch();  //
	}
	
	/**
	 * <p>java method describe</p>
	 * @author Zeven on 2010-7-1
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	}

    /** Returns an ImageIcon, or null if the path was invalid. */
	public static ImageIcon createImageIcon(String path) {
        //java.net.URL imgURL = MainFrame.class.getResource(path);
    	String imgURL = path;
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else { 
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
                                                                              
    
   /**
	* 隐藏JTable中不需要显示的列
	* @param table 需要隐藏列的JTable
	* @param colIndex 需要隐藏的列的下标（JTable列下标从0开始）
	*/
	public static void hideTableColumns(JTable table, int[] colIndex) {

	   DefaultTableColumnModel dcm = (DefaultTableColumnModel) table //获取列模型
	     		.getColumnModel();
	   for (int i = 0; i < colIndex.length; i++) {
		   dcm.getColumn(colIndex[i]).setPreferredWidth(0);
		   dcm.getColumn(colIndex[i]).setMinWidth(0);
		   dcm.getColumn(colIndex[i]).setMaxWidth(0);
	   }

	}

	/**
	 * <p>scroll table to row.</p>
	 * @author Zeven on 2011-3-30
	 * @param table JTable showing
	 * @param row begin from 0 to (rowCount-1)
	 */
    public static void   scrollToRow(JTable table, int row){
    	if (table==null) {
			return;
		}
        int   value   =   table.getRowHeight()*row+1;
        ((JScrollPane)table.getParent().getParent()).getVerticalScrollBar().setValue(value);
    }

    public static void openTabpage(JTabbedPane tabbedPane,int... indexs) {
		 if (indexs==null) {
			return;
		 }
		 if (indexs.length>0) {
			 tabbedPane.setSelectedIndex( indexs[0] );	
		 }
		 	
	}

	public static void selectMatchedRows(JTable table, int columnIndex, Object object) {
		if (object==null) {
			return ;
		}
		int rowCount = table.getRowCount();
		for (int i = 0; i < rowCount; i++) {
			if (object.equals(table.getValueAt(i, columnIndex))) {
				table.addRowSelectionInterval(i, i);
			}
		}
	
	}
	
	public static boolean containsHtmlCode(String src){
		if (src==null) {
			return false;
		}
		String srcLcase = src.toLowerCase(); 
		return (srcLcase.indexOf("<html>")>-1 && srcLcase.indexOf("</html>")>-1)
			|| (srcLcase.indexOf("<font ")>-1 && srcLcase.indexOf("</font>")>-1)
			|| (srcLcase.indexOf("<a ")>-1 && srcLcase.indexOf("</a>")>-1)
			|| (srcLcase.indexOf("<b>")>-1 && srcLcase.indexOf("</b>")>-1)
			|| (srcLcase.indexOf("<i>")>-1 && srcLcase.indexOf("</i>")>-1)
			|| (srcLcase.indexOf("<pre>")>-1 && srcLcase.indexOf("</pre>")>-1)
			|| (srcLcase.indexOf("<center>")>-1 && srcLcase.indexOf("</center>")>-1);
	}
	
	public static void sleepQuietly(long millis){
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			log.error(e);
		}
	}
	
	public static void move2Centre(Component obj){

	    //定位到屏幕中央
	    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	    Dimension fs = obj.getSize();
	    if (fs.height > screenSize.height) fs.height = screenSize.height;
	    if (fs.width > screenSize.width)   fs.width = screenSize.width;
	    obj.setLocation((screenSize.width - fs.width) / 2,
	                      (screenSize.height - fs.height) / 2);
	}
}
